home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5831 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 'Freeing' storage with realloc
  5. Date: 16 Feb 1996 00:01:20 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824446299@pegasus.montclair.edu>
  8. References: <4fv44f$p2v@news.rz.uni-passau.de>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. Keywords: realloc, memory-managment
  11. X-Newsreader: NN version 6.5.0 #68 (NOV)
  12.  
  13. berndl@sidonius.uni-passau.de (Klaus Berndl) writes:
  14. >  char* buf = NULL;
  15.  
  16. >  buf = (char*)malloc(100);
  17. >  strcpy(buf, "Klaus Berndl");
  18. >  printf("\n%s\n", buf);
  19.  
  20. >/* line 10 */  buf = (char*)realloc(buf, strlen(buf)+1);  /* line 10 */
  21.  
  22. >My questions are now: How much memory is allocated for 'buf' after
  23. >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
  24. >Or are still 100 bytes allocated for buf?
  25.  
  26.    Your first premise is correct, realloc() will resize the block of memory
  27. allocated to buf to be 13 characters (strlen(buf) + 1).  The remaining 87
  28. bytes will be freed.  In all likelihood (since buf was involved in the only
  29. memory allocations in your code), those 87 bytes will be reunited with the
  30. heap (no fragmentation will result) and buf and the contents of the memory
  31. buf points to will remain at its present location (no copying to a new location
  32. will be neccessary, as would be the case if your requested > 100 bytes and
  33. other data elements occupied the heap). 
  34.                                                  -- Stone
  35. --
  36. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  37. # - Computer Science Undergrad, Montclair State University, NJ
  38. # - My views are my own, nobody else is this creative.  3;)>
  39. ... Word for Windows, from the people who brought you EDLIN.
  40.  
  41.